package cn.zhouyafeng.itchat4j;

import cn.zhouyafeng.WatherMain;
import cn.zhouyafeng.itchat4j.beans.BaseMsg;
import cn.zhouyafeng.itchat4j.core.Core;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;
import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 图灵机器人示例
 * 
 * @author https://github.com/yaphone
 * @date 创建时间：2017年4月24日 上午12:13:26
 * @version 1.0
 *
 */
public class TulingRobot implements IMsgHandlerFace {
	public WatherMain watherMain;

	static MyHttpClient myHttpClient = Core.getInstance().getMyHttpClient();
	static String url = "http://openapi.tuling123.com/openapi/api/v2";
	static String apiKey = "902e4ebe83e54056b292e0698be3fb14"; // 这里是我申请的图灵机器人API接口，每天只能5000次调用，建议自己去申请一个，免费的:)
	static class Perception {
		public Map<String,String> inputText;
		public Perception(String str) {
			HashMap<String, String> map = new HashMap<>();
			map.put("text", str);
			this.inputText = map;
		}
	}
	static class UserInfo {
		public String apiKey = TulingRobot.apiKey;
		public String userId = new Random().nextInt(9000000) + "";

	}
	static class Msg {
		public int reqType = 0;
		public Perception perception;
		public UserInfo userInfo = new UserInfo();
		public Msg(String msg) {
			this.perception = new Perception(msg);
		}
	}



	public static void main(String[] args) {
//		IMsgHandlerFace msgHandler = new TulingRobot();
//		Wechat wechat = new Wechat(msgHandler, "D://itchat4j/login");
//		wechat.start();
		TulingRobot tulingRobot = new TulingRobot();
		String 李四 = tulingRobot.tulin("李四");

	}

	@Override
	public String textMsgHandle(BaseMsg msg) {
		String text = msg.getText();

		if (text.indexOf("喝水提醒") != -1) {
			return watherMain.getNow();
		}
		return tulin(text);
	}

	private String tulin(String text) {
		String result = "";
		String paramStr = JSON.toJSONString(new Msg(text));
		try {
			HttpEntity entity = MyHttpClient.getInstance().doPost(url, paramStr);
			result = EntityUtils.toString(entity, "UTF-8");
			JSONObject obj = JSON.parseObject(result);
			if (obj.getJSONObject("intent").getIntValue("code") == 4003) {
				return null;
			}
			result = obj.getJSONArray("results").getJSONObject(0).getJSONObject("values").getString("text");
		} catch (Exception e) {
		}
		return result;
	}

	@Override
	public String picMsgHandle(BaseMsg msg) {

		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String voiceMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String viedoMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String nameCardMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void sysMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
	}

	@Override
	public String verifyAddFriendMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String mediaMsgHandle(BaseMsg msg) {
		// TODO Auto-generated method stub
		return null;
	}

}